﻿using System;
using System.ComponentModel.Composition;
using System.Linq;
using System.Web.Mvc;
using System.Web.Routing;
using Gestion.Common.Extensions;
using GestionSaludOcupacional.Infrastructure.Mapping;
using GestionSaludOcupacional.Infrastructure.Services;
using GestionSaludOcupacional.Portal.App_Start;
using GestionSaludOcupacional.Portal.Base.Enumerations;
using GestionSaludOcupacional.Portal.Base.ViewModels;

namespace GestionSaludOcupacional.Portal.Controllers
{
    public class CitasController : Controller
    {
        [Import]
        public Facade Facade;

        //
        // GET: /Citas/
        public ActionResult Index()
        {
            return View();
        }

        [HttpGet]
        [OutputCache(Duration = 0)]
        public ActionResult FormJs(string page)
        {
            object data = null;
            var values = new object();
            switch (page)
            {
                case "Crear":
                    values = new { Medicos = Facade.HealthService.ObtenerMedicos().Select(x => new Item { Key = x.Id.ToString(), Value = x.Contacto.Nombres }) };
                    data = new CitasViewModel();
                    break;
                case "Editar":
                    data = new IndividuoViewModel();
                    break;
            }
            var result = new { data, values };
            return Json(result, JsonRequestBehavior.AllowGet);
        }

        [HttpGet]
        [OutputCache(Duration = 0)]
        public ActionResult BuscarIndividuo(string id, string apellido)
        {
            var data = Facade.GetIndividuos(id, apellido);
            return Json(new { data }, JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        public ActionResult SaveCita(CitasViewModel model)
        {
            // Validation Model
            if (ModelState.IsValid)
            {
                var cita = Facade.MappingService.GetModel(model);
                //Session data
                //cita.UsuarioCreacion = new Guid();

                cita.Estado = (int)CitaEstados.EnEspera;
                var isValid = Facade.HealthService.CrearCita(cita);
                // var isValid = true;
                return Json(new { isValid, fecha = model.Fecha.Value.ToLongDateString() });
            }
            return Json(new { isValid = false, message = ModelState.GetErrors() }, JsonRequestBehavior.AllowGet);
        }

        [HttpGet]
        [OutputCache(Duration = 0)]
        public ActionResult ShowCitas(int idIndividuo)
        {
            object userInfo = null;
            var citas = Facade.HealthService.ObtenerCitas(idIndividuo);
            if (citas.Any())
            {
                var info = citas.First().Individuo;
                userInfo = new
                    {
                        UserName = string.Format("{0} {1} {2}", info.Nombres, info.PrimerApellido, info.SegundoApellido),
                        NoId = info.NoIdentificacion
                    };
            }
            var data = Facade.MappingService.GetViewModel(citas).OrderByDescending(x => x.Fecha);
            return Json(new { data, userInfo }, JsonRequestBehavior.AllowGet);
        }

        public ActionResult Activas(string examen)
        {
            ViewBag.Examen = examen;
            var individuos = Facade.MappingService.GetAllViewModel(Facade.HealthService.ObtenerCitasExamen(examen).Select(f => f.Individuo));
            return View(individuos);
        }

        public ActionResult SiguienteCita(string examen)
        {
            var model = Facade.HealthService.ObtenerCitasExamen(examen).FirstOrDefault();
            if (model == null)
                return RedirectToAction("NoCita");
            switch (examen)
            {
                case "General":
                    return RedirectToAction("Index", "Historia", new { id = model.IdIndividuo, citaId = model.Id });
                case "Vision":
                    return RedirectToAction("Vision", "Examen", new { id = model.IdIndividuo, citaId = model.Id });
                case "Audicion":
                    return RedirectToAction("Audicion", "Examen", new { id = model.IdIndividuo, citaId = model.Id });
                case "Psicosensometrico":
                    return RedirectToAction("Psicosensometrico", "Examen", new { id = model.IdIndividuo, citaId = model.Id });
                case "Psicologia":
                    return RedirectToAction("Psicologia", "Examen", new { id = model.IdIndividuo, citaId = model.Id });
            }

            return View("Activas", examen);
        }

        public ActionResult SeleccionarCita(int id, string examen, int citaId)
        {
            switch (examen)
            {
                case "General":
                    return RedirectToAction("Index", "Historia", new { id, citaId });
                case "Vision":
                    return RedirectToAction("Vision", "Examen", new { id, citaId });
                case "Audicion":
                    return RedirectToAction("Audicion", "Examen", new { id, citaId });
                case "Psicosensometrico":
                    return RedirectToAction("Psicosensometrico", "Examen", new { id, citaId });
                case "Psicologia":
                    return RedirectToAction("Psicologia", "Examen", new { id, citaId });

            }
            return View("Activas", examen);
        }

        public ActionResult NoCita()
        {
            return View();
        }
    }
}
